import csv
import os
import sys



class data:
    def __init__(self, csv_file):
        self.csv_file = csv_file
        self.entries = {};
        self.headers = [];
    
    def create(self):
        f = open(self.csv_file, 'rU')
        infile = csv.reader(f, delimiter=',', lineterminator='\n')
        
        fileEntries = [];
        
        for row in infile:
            fileEntries.append(row)
        
        headers = fileEntries[0];
        self.headers = headers;
        
        for x in headers:
            head = x;
            self.entries[head] = [];
            
        for i in range(1, len(fileEntries)):
            for j in range(len(headers)):
                try:
                    self.entries[headers[j]].append(float(fileEntries[i][j]));
                except ValueError:
                    self.entries[headers[j]].append(fileEntries[i][j]);
        f.close()
        return self
    
    
    '''Since we've complicated the data structure so much, we really need a simple way to get a row vector of input.  If given a 
    vector of names to use, use those, otherwise iterate over all of the possible headers and build a vector from that.''' 
    def fillDataForIndex(self, index, dataMap):
        row = [];
        for key in dataMap:
            dataMap[key].setValue(self.entries[key][index]);
            row.append(dataMap[key].getValue());
        return row;
    
    '''Convenience method for establishing the number of rows '''
    def __len__(self):
        length =len(self.entries[self.headers[0]]); 
        return length;
    
    '''Convenience method for pulling out the known rating'''
    def getRating(self, index):
        return self.entries["rating"][index];
        
if __name__ == '__main__':
    if len(sys.argv) > 1:
        csv_file = sys.argv[1]
        num_cols = sys.argv[2]
        mydata = data(csv_file, num_cols)
        entries = mydata.create()
